﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.ProviderBase;
using System.Data;
using System.Text;
using System.Drawing;
using PMPUtils.Report;
using PMPUtils.Form;

namespace PMP.Reports
{
    public partial class SLA : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FormUtil.Authenticate(Session, Response);

            if (!IsPostBack)
            {
                try
                {
                    DataTable dtSLA = SLAOperator.ReadSLA();

                    grvSLA.DataSource = dtSLA.DefaultView;
                    grvSLA.DataBind();

                    FormatGridView();
                }
                catch (Exception ex)
                {
                    Messenger.Show(Response, "Error: " + ex.Message);
                    return;
                }
            }
        }

        private void FormatGridView()
        {
            foreach (GridViewRow row in grvSLA.Rows)
            {
                int cellID = GridViewOperator.GetCellID(grvSLA, "RemainDays");
                double remainingDays = GridViewOperator.GetCellDoubleValue(grvSLA, row, "RemainDays");

                if (remainingDays <= 1)
                {
                    row.Cells[cellID].BackColor = Color.Red;
                }
                else if (remainingDays <= 3)
                {
                    row.Cells[cellID].BackColor = Color.Yellow;
                }
                else
                {
                    row.Cells[cellID].BackColor = Color.Green;
                }
            }
        }

        protected void grvSLA_Sorted(object sender, EventArgs e)
        {
            FormatGridView();
        }
    }
}
